//
//  RadioStationViewController.m
//  GSG
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RadioStationViewController.h"
#import "AFNetworking.h"
#import "MusicModel.h"
#import "UIImageView+WebCache.h"
#import "RadioCollectionViewCell.h"
#import "PlayerViewController.h"
@interface RadioStationViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,retain)NSMutableArray *modelArr;
@property(nonatomic,retain)NSMutableDictionary *FMDic;
@property (nonatomic,retain)UICollectionView *myCollectionView;
@property (nonatomic,retain)UICollectionReusableView *headView;
@end

@implementation RadioStationViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"电台";
        self.modelArr = [NSMutableArray array];
        self.FMDic = [NSMutableDictionary dictionary];
    }
    
    return self;
}

- (void)data{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://fm.api.ttpod.com/radiolist?image_type=240_200&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone4&f=f320&s=s310&imsi=&hid=&splus=7.1.1&active=1&net=2&openudid=ad6f50f586818c4f36ff4358053c1b53fee58cdf&idfa=E2EAC64B-1F1C-4AC5-A0E1-8BE37A03FB96&utdid=VTCyBQSkYzwDAALO9zZTFfPC&alf=201200" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {


        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [NSMutableArray array];
        
        arr = dic[@"data"];
        for (NSInteger i = 0; i < arr.count; i++) {
            MusicModel *fmModel = [[MusicModel alloc] init];
            [fmModel setValuesForKeysWithDictionary:arr[i]];
            [self.modelArr addObject:fmModel.tag_type_name];
            [self.FMDic setObject:[NSMutableArray array] forKey:fmModel.tag_type_name];
            for (NSMutableDictionary *tempDic in fmModel.data) {
                MusicModel *fmModel2 = [[MusicModel alloc] init];
                [fmModel2 setValuesForKeysWithDictionary:tempDic];
                [[self.FMDic objectForKey:fmModel.tag_type_name] addObject:fmModel2];
                [fmModel2 release];
            }
            [fmModel release];
        }
        
        
        [self.myCollectionView reloadData];
        
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"No");
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self  data];
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowlayout.itemSize = CGSizeMake(self.view.frame.size.width * 100 / 375, self.view.frame.size.height * 150 / 667);
    
    flowlayout.minimumLineSpacing = 10;
    
    flowlayout.minimumInteritemSpacing = 10;
    
    flowlayout.headerReferenceSize = CGSizeMake(0, self.view.frame.size.height * 40 / 667);
    flowlayout.footerReferenceSize = CGSizeMake(0, 0);
    
    flowlayout.sectionInset = UIEdgeInsetsMake(self.view.frame.size.width * 20 / 375, self.view.frame.size.height * 20 / 667, self.view.frame.size.width * 20 / 375, self.view.frame.size.height * 20 / 667);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowlayout];
    [self.view addSubview:self.myCollectionView];
    self.myCollectionView.backgroundColor = [UIColor clearColor];
    
    
    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    
    
    [self.myCollectionView registerClass:[RadioCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    [self.myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    
//    [self.myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
    
    self.headView = [[UICollectionReusableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 20 / 375, self.view.frame.size.height * 5 / 667, self.view.frame.size.width * 60 / 375, self.view.frame.size.height * 20 / 667)];
//    self.headView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return [[self.FMDic objectForKey:self.modelArr[section]] count];
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RadioCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    NSArray *arr = [self.FMDic objectForKey:self.modelArr[indexPath.section]];
    MusicModel *fmModel = [[MusicModel alloc] init];
    fmModel = arr[indexPath.row];
    NSURL *url = [NSURL URLWithString:fmModel.pic_url_240_200];
    [cell.myImageView sd_setImageWithURL:url];
    cell.quantityLabel.text = [NSString stringWithFormat:@"%@首", fmModel.quantity];
    cell.myLabel.text = fmModel.tag_name;
//    cell.smallImageView.image = [UIImage imageNamed:@"m3.jpg"];
    [fmModel release];
    return cell;
    

  
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
//    NSLog(@"%ld",self.modelArr.count);
    return self.modelArr.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{


    
    UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"head" forIndexPath:indexPath];
//    headView.frame= CGRectMake(0, 0, self.view.frame.size.width, 30);
    headView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 20 / 375, self.view.frame.size.height * 5 / 667, self.view.frame.size.width * 60 / 375, self.view.frame.size.height * 20 / 667)];
    label.backgroundColor = [UIColor colorWithRed:71/255.0 green:175/255.0 blue:175/255.0 alpha:1];
    label.text = self.modelArr[indexPath.section];
    [headView addSubview:label];
    [label release];
    
//    headView.headTextLabel.text = self.modelArr[indexPath.section];
    
//    if (indexPath.section == 0)
//    {
//        label.text = @"热门";
//    }else if (indexPath.section == 1){
//        label.text = @"场景";
//    }else if (indexPath.section == 2){
//        label.text = @"年代";
//    }else if (indexPath.section == 3){
//        label.text = @"心情";
//    }else if (indexPath.section == 4){
//        label.text = @"语言";
//    }else if (indexPath.section == 5){
//        label.text = @"风格";
//    }else if (indexPath.section == 6){
//        label.text = @"特色";
//    }
    

    
 return headView;
    
}




- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"点击%ld",indexPath.row);
    PlayerViewController *pvc=[[PlayerViewController alloc]init];
    [self.navigationController pushViewController:pvc animated:YES];
    
    
    
    [pvc release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
